package com.example.snowli.fragments;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.TestLooperManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.example.snowli.EditActivity;
import com.example.snowli.MainActivity;
import com.example.snowli.R;
import com.example.snowli.adapter.SelectWordAdapter;

import java.util.ArrayList;
import java.util.List;

public class SelectFragment extends Fragment {

    private SQLiteDatabase db;

    private EditText edtSearch;
    private Button btnSearch;
    private ListView listWords;

    //listview 适配器
    private SelectWordAdapter selectWordAdapter;

    private List<String> wordsList;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View selectPage = inflater.inflate(R.layout.activity_select_fragment,null);

        db = MainActivity.db;
        //绑定控件
        edtSearch = selectPage.findViewById(R.id.edt_search);
        btnSearch = selectPage.findViewById(R.id.btn_search);
        listWords = selectPage.findViewById(R.id.list_words);


        //点击查询按钮开始查询
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (edtSearch.getText().toString()!=null){
                    selectWords();
                } else {
                    //弹窗提示 没有所查询的单词
                    new AlertDialog.Builder(getContext())
                            .setTitle("请输入您要查询的单词")
                            .setMessage("")
                            .show();
                }
            }
        });


        //点击单词列表跳转到新页面，对单词进行编辑
        listWords.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(
                        getActivity(),
                        EditActivity.class
                );
                String wordAndTran = wordsList.get(position);
                intent.putExtra("wordAndT",wordAndTran);

                startActivity(intent);


            }
        });



        return selectPage;

    }



    private void selectWords() {
        //查询数据 进行模糊查询
//        select * from words where word like "%example%";
        String key = edtSearch.getText().toString();
        String[] columns = {"id","word","tran"};
        String selection = "word like ?";
        String[] selectionArgs = {"%"+ key +"%"};
        Cursor cursor = db.query(
                "words",
                columns,
                selection,
                selectionArgs,
                null,null,null);

        wordsList = new ArrayList<>();
        while (cursor.moveToNext()){
            int idindex = cursor.getColumnIndex("id");
            int id = cursor.getInt(idindex);
            int wordIndex = cursor.getColumnIndex("word");
            String word = cursor.getString(wordIndex);
            int tranIndex = cursor.getColumnIndex("tran");
            String tran = cursor.getString(tranIndex);
            wordsList.add(id+":"+word+":"+tran);
        }

        //如果查询的是空
        if (wordsList.isEmpty()){
            //弹窗提示 没有所查询的单词
            new AlertDialog.Builder(getContext())
                    .setTitle("没有您想要查询的单词")
                    .setMessage("")
                    .show();
        }


        //绑定适配器，放入listview中
        //实例化适配器对象
        selectWordAdapter = new SelectWordAdapter(
                getActivity(),
                R.layout.activity_select_item,
                wordsList
        );
        //绑定适配器
        listWords.setAdapter(selectWordAdapter);






    }






}

